<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Role extends Model
{
    use HasFactory;
    public $timestamps = false;
    protected $table = 'ad_role';

    //获取到用户角色列表
    public static function getManageRole($manage_id)
    {
/*        $field = ['mr.role_id as role_id','r.name as role_name','r.descr as role_descr'];
        return DB::table("ad_manage_role","mr")
            ->leftJoin("ad_role as r","mr.role_id","=","r.id")
            ->where($where)
            ->select($field)
            ->get();*/
        $data = self::query()->get();
        if(!empty($manage_id)){
            //获取到用户拥有的角色
            $manage_role = ManageRole::query()->where('manage_id',$manage_id)->get()->toArray();
            $role_ids = array_column($manage_role,'role_id');
            foreach ($data as &$item){
                if(in_array($item->id,$role_ids)){
                    $item->is_show = true;
                }else{
                    $item->is_show = false;
                }
            }
        }
        return $data;
    }
}
